package com.cc.networkmonitor;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.util.Log;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;

/**
 * WIFI 状态监听器
 */
public class WifiStateMonitor {
    private static final String TAG = "WifiStateMonitor";
    private Context context;
    private WifiStateCallback wifiStateCallback;
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
                    Log.i(TAG, "wifiState:" + wifiState);
                    if (wifiStateCallback != null) {
                        wifiStateCallback.onWifiStateChanged(wifiState);
                    }
                    switch (wifiState) {
                        case WifiManager.WIFI_STATE_DISABLED:
                            Log.i(TAG, "wifiState:WIFI_STATE_DISABLED");
                            if (wifiStateCallback != null) {
                                wifiStateCallback.onWifiDisable();
                            }
                            break;
                        case WifiManager.WIFI_STATE_DISABLING:
                            Log.i(TAG, "wifiState:WIFI_STATE_DISABLING");
                            if (wifiStateCallback != null) {
                                wifiStateCallback.onWifiDisabling();
                            }
                            break;
                        case WifiManager.WIFI_STATE_ENABLED:
                            Log.i(TAG, "wifiState:WIFI_STATE_ENABLED");
                            if (wifiStateCallback != null) {
                                wifiStateCallback.onWifiEnabled();
                            }
                            break;
                        case WifiManager.WIFI_STATE_ENABLING:
                            Log.i(TAG, "wifiState:WIFI_STATE_ENABLING");
                            if (wifiStateCallback != null) {
                                wifiStateCallback.onWifiEnabling();
                            }
                            break;
                        case WifiManager.WIFI_STATE_UNKNOWN:
                            Log.i(TAG, "wifiState:WIFI_STATE_UNKNOWN");
                            if (wifiStateCallback != null) {
                                wifiStateCallback.onWifiStateUnknown();
                            }
                            break;
                    }
                }
            }
        }
    };

    /**
     * 开始监听网络状态，无需解注册
     *
     * @param activity          界面
     * @param wifiStateCallback 回调
     */
    public void startAuto(Activity activity, WifiStateCallback wifiStateCallback) {
        this.context = activity;
        this.wifiStateCallback = wifiStateCallback;
        activity.registerReceiver(receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
        activity.getApplication().registerActivityLifecycleCallbacks(new SimpleLifeCycleCallBack() {
            @Override
            public void onActivityDestroyed(@NonNull Activity act) {
                if (act == activity) {
                    act.unregisterReceiver(receiver);
                    act.getApplication().unregisterActivityLifecycleCallbacks(this);
                }
            }
        });
    }

    /**
     * 开始监听
     *
     * @param context           上下文
     * @param wifiStateCallback Wifi监听回调
     */
    public void start(Context context, WifiStateCallback wifiStateCallback) {
        if (context instanceof Activity) {
            startAuto((Activity) context, wifiStateCallback);
        } else {
            context.registerReceiver(receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
        }
    }

    /**
     * 解注册
     */
    public void unRegister() {
        if (!(context instanceof Activity)) {
            context.unregisterReceiver(receiver);
        }
    }

    /**
     * WIFI 状态回调
     */
    public abstract static class WifiStateCallback {
        public void onWifiDisable() {
        }

        public void onWifiDisabling() {
        }

        public void onWifiEnabled() {
        }

        public void onWifiEnabling() {
        }

        public void onWifiStateUnknown() {
        }

        public void onWifiStateChanged(@WifiStateDef int wifiState) {
        }
    }

    @IntDef({WifiManager.WIFI_STATE_DISABLED, WifiManager.WIFI_STATE_DISABLING, WifiManager.WIFI_STATE_ENABLED, WifiManager.WIFI_STATE_ENABLING, WifiManager.WIFI_STATE_UNKNOWN})
    @interface WifiStateDef {
    }
}
